$(document).ready(function () {
 var Addr = window.location.href;

    if (Addr == 'http://www.themediaant.com/p/advertisers.html' || Addr == 'http://www.themediaant.com/p/media-owners.html' || Addr == 'http://www.themediaant.com/p/about-us.html' || Addr == 'http://www.themediaant.com/p/contact-us_11.html') {
        if (Addr.indexOf('search') == -1) {
            $('.tester').hide();
        }
    }
    else {

        $('.tester').show();
    }

   // if (Addr == 'http://www.themediaant.com') {
  //      $('#filters').show();
  //  }
  //  else {
   //     $('#filters').hide();
   // }
    var $ui = $('#ui_Media');
    var $ui2 = $('#ui_Sex');
    var $ui3 = $('#ui_Town');
    var $ui4 = $('#ui_Age');
    var filterOne = new Array();
    var filterTwo = new Array();
    var filterThree = new Array();
    var filterFour = new Array();

    $(document).mousedown(function (event) {
        var target = $(event.target);
        if (!target.parents().andSelf().hasClass('sb_DropDownFilters')) {
            $('.sb_DropDownFilters').hide();
        }
    });

    $ui.find('#btnMediaType').bind('click', function () {
        $ui.find('#upDownIncome')
          .addClass('sb_down')
          .removeClass('sb_up')
          .andSelf().find('#UlMedia').show(300);

    });



    /*/////type 2 ////*/
    $ui2.find('#btnSex').bind('click', function () {
        $ui2.find('#upDownSex')
          .addClass('sb_up')
          .removeClass('sb_down')
          .andSelf()
          .find('#UlSex')
          .show();
    });



    //////type 3 ////

    $ui3.find('#btnTown').bind('click', function () {
        $ui3.find('#upDownTown')
          .addClass('sb_up')
          .removeClass('sb_down')
          .andSelf()
          .find('#UlTown')
          .show();

    });
    /////type 4

    $ui4.find('#btnAge').bind('click', function () {
        $ui4.find('#upDownAge')
          .addClass('sb_up')
          .removeClass('sb_down')
          .andSelf()
          .find('#UlAge')
          .show();
    });

    function mapArray(filterType) {

        if (filterType == "chkOOHScreens")
            return chkOOHScreens;
        else if (filterType == "chkBillsTickets")
            return chkBillsTickets;
        else if (filterType == "chkInnovative")
            return chkInnovative;
        else if (filterType == "chkAmbient")
            return chkAmbient;
        else if (filterType == "chkOnline")
            return chkOnline;
        else if (filterType == "chkTransit")
            return chkTransit;
        else if (filterType == "chkSampling")
            return chkSampling;
        else if (filterType == "chkMobile")
            return chkMobile;
        else if (filterType == "chkDatabaseMktg")
            return chkDatabaseMktg;
    }

    function ApplyMediaFilter(filterType) {

        var tempArr = new Array();
        tempArr = mapArray(filterType);

        for (var i = 0; i < tempArr.length; i++) {
            filterOne.push(tempArr[i].ID);
        }
        var finalArray = new Array();
        finalArray = GetUniqueArray(filterOne);
        hideAllMedia();
        for (var ctr = 0; ctr < finalArray.length; ctr++) {
            var temp = "#" + finalArray[ctr];
            $(temp).show();
        }
    }

    function GetUniqueArray(toFindArray) {
        var o = {}, i, l = toFindArray.length, r = [];
        for (i = 0; i < l; i += 1) o[toFindArray[i]] = toFindArray[i];
        for (i in o) r.push(o[i]);
        return r;
    }


    function RemoveMediaFilter() {

        while (filterOne.length != 0) {
            filterOne.pop();
        }
        $(".chkboxMedia:checked").each(function () {
            ApplyMediaFilter($(this).attr('id'));
        });
    }



    $('.chkboxMedia').bind('click', function () {

        if ($(this).is(':checked')) {
            createLabel($(this).val());

            ApplyMediaFilter($(this).attr('id'));
        }
        else {
            removeLabelByTitle($(this).val());
            RemoveMediaFilter();
        }

        if ($(".chkboxMedia:checked").length == 0) {
            showAllMedia();
        }
    });

    $('.lblMedia').bind('click', function () {

        if ($(this).prev().is(':checked')) {
            $(this).prev().attr('checked', false);
            removeLabelByTitle($(this).prev().val());
            RemoveMediaFilter();
        }
        else {
            $(this).prev().attr('checked', true);
            ApplyMediaFilter($(this).prev().attr('id'));
            createLabel($(this).prev().val());
        }


        if ($(".chkboxMedia:checked").length == 0) {
            showAllMedia();
        }


    });

    function createLabel(title) {
        var div = document.createElement("div");
        div.setAttribute("id", title);
        var span = document.createElement("span");
        span.setAttribute("class", "crossimage");
        var label = document.createElement("input");
        //Assign different attributes to the element.
        label.setAttribute("type", "button");
        label.setAttribute("class", "smallBox");
        label.setAttribute("id", title);
        label.setAttribute("value", title);
        div.appendChild(label);
        div.appendChild(span);
        var foo = document.getElementById("selected_filter");
        foo.appendChild(div);
    }


    function hideAllMedia() {


        for (var i = 0; i < All.length; i++) {
            var Media = '#' + All[i].ID;
            $(Media).hide();
        }
    }


    function showAllMedia() {


        for (var i = 0; i < All.length; i++) {
            var Media = '#' + All[i].ID;
            $(Media).show();
        }
    }


    function removeCheck(title) {

        var chkbox = "#chk" + title;
        chkbox = chkbox.replace(' ', '');
        chkbox = chkbox.replace('/', '');

        if ($(chkbox).is(':checked')) {
            $(chkbox).attr('checked', false);
        }
    }


    $('.smallBox').live('click', function () {

        removeCheck($(this).val());
        removeLabel($(this));
        RemoveMediaFilter();
        if ($(".chkboxMedia:checked").length == 0) {
            showAllMedia();
        }
    });

    function removeLabel(title) {

        $(title).remove();
    }



    function removeLabelByTitle(title) {

        var parent = document.getElementById('selected_filter');
        var toRemove = document.getElementById(title);
        parent.removeChild(toRemove);


    }

});
